using System;
using Avalonia.Controls;
using BruTile.Predefined;
using Mapsui.Layers;
using Mapsui.Projections;
using Mapsui.Styles;
using Mapsui.Tiling.Layers;

namespace Mapsui.InfiniteMap.Views;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        
        var mapControl = new Mapsui.UI.Avalonia.MapControl();

        // 地图展示内容
        mapControl.Map.Layers.Add(CreateBingLayer()); // 瓦片地图
        mapControl.Map.Layers.Add(CreatePointLayer()); // 点
        
        mapControl.Map.Navigator.Limiter = new HeightLimiter();
        mapControl.SetMapRenderer(new InfiniteMapRender());
        
        Content = mapControl;
    }
    
    private static TileLayer CreateBingLayer()
    {
        var tileSource = KnownTileSources.Create(KnownTileSource.BingHybrid);
        
        return new TileLayer(tileSource)
        {
            Name = "Bing Aerial",
        };
    }

    private static MemoryLayer CreatePointLayer()
    {
        var layer = new MemoryLayer("点");

        layer.Features = 
        [
            AddPoint(layer, 105, 40),
            AddPoint(layer, 100, 30),
            AddPoint(layer, 117, 32),
        ];
        
        return layer;
    }

    private static PointFeature AddPoint(MemoryLayer layer, double lon, double lat)
    {
        var (x, y) = SphericalMercator.FromLonLat(lon, lat);
        var feature = new PointFeature(x,y) ;
        
        // 添加样式
        var path = "file:///" + AppDomain.CurrentDomain.BaseDirectory + $"Assets/MpaPoint.png";
        feature.Styles = 
        [
            new ImageStyle() { Image = new Mapsui.Styles.Image { Source = path } },
        ];
        
        return feature;
    }
}